﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:TaskDo
    /// </summary>
    public partial class TaskDo
    {
        /// <summary>
        /// 重点督查（文件阅办）处理
        /// </summary>
        public TaskDo()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="FTaskDoID">处理类编号</param>
        /// <param name="FTaskID">重点督查（文件阅办）编号</param>
        /// <returns>是否存在记录</returns>
        public bool Exists(decimal FTaskDoID, decimal FTaskID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_TaskDo");
            strSql.Append(" where FTaskDoID=@FTaskDoID and FTaskID=@FTaskID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskDoID", SqlDbType.Decimal,13),
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13)			};
            parameters[0].Value = FTaskDoID;
            parameters[1].Value = FTaskID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条记录
        /// </summary>
        /// <param name="model">处理结果记录</param>
        /// <param name="taskModel">改变主记录状态</param>
        public void check(ElingOA6_Admin_DSJCJ.Model.TaskDo model, Model.Task taskModel)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("insert into dbo.t_TaskDo(");
            strSql.Append("FTaskID,FEntityNo,FEmplNo,FNote,FStatus,FInsDate,FInsIP)");
            strSql.Append(" values (");
            strSql.Append("@FTaskID,@FEntityNo,@FEmplNo,@FNote,@FStatus,@FInsDate,@FInsIP)");
            strSql.Append(";select @returnBId=IDENT_CURRENT('dbo.t_TaskDo')");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FNote", SqlDbType.NVarChar,-1),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,20),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
                    new SqlParameter("@returnBId", SqlDbType.Decimal)};
            parameters[0].Value = model.FTaskID;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FEmplNo;
            parameters[3].Value = model.FNote;
            parameters[4].Value = model.FStatus;
            parameters[5].Value = model.FInsDate;
            parameters[6].Value = model.FInsIP;
            parameters[7].Direction = ParameterDirection.Output;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            if (taskModel != null)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_Task set ");
                strSql.Append("FEndDate=@FEndDate,");
                strSql.Append("FStatus=@FStatus,");
                strSql.Append("FCheckDate=@FCheckDate,");
                strSql.Append("FCheckIP=@FCheckIP,");
                strSql.Append("FCheckEmpNo=@FCheckEmpNo");
                strSql.Append(" where FTaskID=@FTaskID");
                SqlParameter[] parameters3 = {
					new SqlParameter("@FStatus", SqlDbType.NVarChar,20),
                    new SqlParameter("@FEndDate", SqlDbType.NVarChar,20),
                    new SqlParameter("@FCheckEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckDate", SqlDbType.DateTime),
					new SqlParameter("@FCheckIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13)};
                parameters3[0].Value = taskModel.FStatus;
                parameters3[1].Value = taskModel.FEndDate;
                parameters3[2].Value = taskModel.FCheckEmpNo;
                parameters3[3].Value = taskModel.FCheckDate;
                parameters3[4].Value = taskModel.FCheckIP;
                parameters3[5].Value = taskModel.FTaskID;

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters3;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">增加处理记录</param>
        /// <param name="taskAttachModel">附件记录</param>
        /// <param name="taskModel">改变主记录状态</param>
        public void Add(ElingOA6_Admin_DSJCJ.Model.TaskDo model, Model.TaskAttach taskAttachModel, Model.Task taskModel)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("insert into dbo.t_TaskDo(");
            strSql.Append("FTaskID,FEntityNo,FEmplNo,FNote,FStatus,FInsDate,FInsIP)");
            strSql.Append(" values (");
            strSql.Append("@FTaskID,@FEntityNo,@FEmplNo,@FNote,@FStatus,@FInsDate,@FInsIP)");
            strSql.Append(";select @returnBId=IDENT_CURRENT('dbo.t_TaskDo')");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FNote", SqlDbType.NVarChar,-1),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,20),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
                    new SqlParameter("@returnBId", SqlDbType.Decimal)};
            parameters[0].Value = model.FTaskID;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FEmplNo;
            parameters[3].Value = model.FNote;
            parameters[4].Value = model.FStatus;
            parameters[5].Value = model.FInsDate;
            parameters[6].Value = model.FInsIP;
            parameters[7].Direction = ParameterDirection.Output;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            if (taskAttachModel != null)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_TaskAttach(");
                strSql.Append("FAttachSvrFileName,FAttachDesc,FAttachContent,FAttachFileExt,FEntityNo,FAttachOldFileName,FTaskDoID)");
                strSql.Append(" values (");
                strSql.Append("@FAttachSvrFileName,@FAttachDesc,@FAttachContent,@FAttachFileExt,@FEntityNo,@FAttachOldFileName,@FTaskDoID)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachDesc", SqlDbType.NVarChar,20),
					new SqlParameter("@FAttachContent", SqlDbType.NVarChar,20),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FTaskDoID", SqlDbType.Decimal,13)};
                parameters2[0].Value = taskAttachModel.FAttachSvrFileName;
                parameters2[1].Value = taskAttachModel.FAttachDesc;
                parameters2[2].Value = taskAttachModel.FAttachContent;
                parameters2[3].Value = taskAttachModel.FAttachFileExt;
                parameters2[4].Value = taskAttachModel.FEntityNo;
                parameters2[5].Value = taskAttachModel.FAttachOldFileName;
                parameters2[6].Direction = ParameterDirection.InputOutput;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }


            if (taskModel != null)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_Task set ");
                strSql.Append("FStatus=@FStatus");
                strSql.Append(" where FTaskID=@FTaskID");
                SqlParameter[] parameters3 = {
					new SqlParameter("@FStatus", SqlDbType.NVarChar,20),
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13)};
                parameters3[0].Value = taskModel.FStatus;
                parameters3[1].Value = taskModel.FTaskID;

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters3;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }
        

        /// <summary>
        ///  更新一条数据
        /// </summary>
        /// <param name="model">更新实体类</param>
        /// <returns>是否更新成功</returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.TaskDo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_TaskDo set ");
            strSql.Append("FNote=@FNote,");
            strSql.Append("FStatus=@FStatus,");
            strSql.Append("FInsDate=@FInsDate,");
            strSql.Append("FInsIP=@FInsIP");
            strSql.Append(" where FTaskDoID=@FTaskDoID");
            SqlParameter[] parameters = {
					new SqlParameter("@FNote", SqlDbType.NVarChar,-1),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,20),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FTaskDoID", SqlDbType.Decimal,13),
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FNote;
            parameters[1].Value = model.FStatus;
            parameters[2].Value = model.FInsDate;
            parameters[3].Value = model.FInsIP;
            parameters[4].Value = model.FTaskDoID;
            parameters[5].Value = model.FTaskID;
            parameters[6].Value = model.FEntityNo;
            parameters[7].Value = model.FEmplNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="FTaskDoID">编号</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(decimal FTaskDoID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_TaskDo ");
            strSql.Append(" where FTaskDoID=@FTaskDoID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskDoID", SqlDbType.Decimal)
			};
            parameters[0].Value = FTaskDoID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
     
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="fTaskDoID">双主键处理编号</param>
        /// <param name="fTaskID">双主键主表编号</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(decimal fTaskDoID, decimal fTaskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_TaskDo ");
            strSql.Append(" where FTaskDoID=@FTaskDoID and FTaskID=@FTaskID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskDoID", SqlDbType.Decimal,13),
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13)			};
            parameters[0].Value = fTaskDoID;
            parameters[1].Value = fTaskID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="fTaskDoIDlist">编号列表</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteList(string fTaskDoIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_TaskDo ");
            strSql.Append(" where FTaskDoID in (" + fTaskDoIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="FTaskDoID">编号</param>
        /// <returns>实体类</returns>
        public ElingOA6_Admin_DSJCJ.Model.TaskDo GetModel(decimal FTaskDoID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FTaskDoID,FTaskID,FEntityNo,FEmplNo,FNote,FStatus,FInsDate,FInsIP from dbo.t_TaskDo ");
            strSql.Append(" where FTaskDoID=@FTaskDoID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskDoID", SqlDbType.Decimal)
			};
            parameters[0].Value = FTaskDoID;

            ElingOA6_Admin_DSJCJ.Model.TaskDo model = new ElingOA6_Admin_DSJCJ.Model.TaskDo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


       /// <summary>
       /// DataRow转换MODEL
       /// </summary>
       /// <param name="row">DataRow对象</param>
       /// <returns>实体类Model</returns>
        public ElingOA6_Admin_DSJCJ.Model.TaskDo DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.TaskDo model = new ElingOA6_Admin_DSJCJ.Model.TaskDo();
            if (row != null)
            {
                if (row["FTaskDoID"] != null && row["FTaskDoID"].ToString() != "")
                {
                    model.FTaskDoID = decimal.Parse(row["FTaskDoID"].ToString());
                }
                if (row["FTaskID"] != null && row["FTaskID"].ToString() != "")
                {
                    model.FTaskID = decimal.Parse(row["FTaskID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FNote"] != null)
                {
                    model.FNote = row["FNote"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns>DataSet对象</returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FTaskDoID,FTaskID,FEntityNo,FEmplNo,FNote,FStatus,FInsDate,FInsIP ");
            strSql.Append(" FROM dbo.t_TaskDo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        /// <param name="Top">前几行数据</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序方式</param>
        /// <returns>DataSet对象</returns>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FTaskDoID,FTaskID,FEntityNo,FEmplNo,FNote,FStatus,FInsDate,FInsIP ");
            strSql.Append(" FROM dbo.t_TaskDo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        ///  获取记录总数
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns>总记录数</returns>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dbo.t_TaskDo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
      
       

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

